<?php
class ArticleController extends AdminController{
    public function indexAction(){
        
    }
    public function createAction(){
        $cate_model = new CategoryModel;
        $cate_data = $cate_model->select();
        $this->_view->category = $cate_data;
    }
    public function insertAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $data['category'] = Request::aspost(3,'category',0);
        $data['title'] = Request::aspost(6,'title','');
        $data['author'] = Request::aspost(6,'author','');
        $data['description'] = Request::aspost(6,'description','');
        $data['contents'] = Request::aspost(6,'contents','');
        $data['tag'] = Request::aspost(6,'tag','');

        $model = new ArticleModel;
        $result = $model->insert($data);
        if($result != 0){
            Response::success("Add the article successly!");
        }else{
            Response::error("Add the article fault!");
        }
    }
    public function browseAction(){
        $art_model = new ArticleModel;
        $art_data = $art_model->order('id desc')->limit(10)->select();
        $rows = $art_model->count();
        $this->_view->art = $art_data;
        $this->_view->rows = $rows;

    }
    public function editAction(){
        
        //get the post;
        $id = Request::asget(3,'id',1);
        $article_model = new ArticleModel;
        $article = $article_model->where(['id'=>$id])->select();
        $this->_view->article = $article[0];
        
        //get all the categorys;
        $cate_model = new CategoryModel;
        $category = $cate_model->select();
        $this->_view->category = $category;
    }
    public function updateAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id',null);
        if($id === 0){
            exit();
        }else{
            
            $data['category'] = Request::aspost(3,'category',0);
            $data['title'] = Request::aspost(6,'title','');
            $data['author'] = Request::aspost(6,'author','');
            $data['description'] = Request::aspost(6,'description','');
            $data['contents'] = Request::aspost(5,'contents','');
            // $data['contents'] = $_POST['contents'];

            $data['tag'] = Request::aspost(6,'tag','');
            // var_dump($data);
            $model = new ArticleModel;
            $result = $model->where(['id'=>$id])->update($data);
            if($result == 0 || $result == false){
                Response::error('error');
            }else{
                Response::success('nice');
            }
        }

    }
    public function statusAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id',null);
        // $status = Request::aspost(3,'status',null);
        $status = $_POST['status'];

        // var_dump($status);
        if($id === null || $status === null){
            return Response::error('error');            
            exit;
        }else{
            if($status == 1){
                $data =[
                    'status' => 0
                ];
                
            }else{
                $data =[
                    'status' => 1
                ];
            }
            $model = new ArticleModel;
            $flag = $model->where(['id'=>$id])->update($data);
            if($flag == 0 || $flag == false){
                return Response::error('error');
            }else{
                return Response::success('nice');
            }
            
        }
    }    
    public function removeAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id',null);
        if($id === null){
            exit;
        }else{
            $model = new ArticleModel;
            $flag = $model->where(['id'=>$id])->delete();
            // var_dump($flag);
            if($flag == 0){
                return Response::success('nice');
                
            }else{
                return Response::error('error');
            }
            
        }
            
    }
    public function pageAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $page = Request::aspost(3,'page',0);
        $art_model = new ArticleModel;
        $art_data = $art_model->order('id DESC')->limit($page,10)->select();
        $data = [
            'art' => $art_data,
        ];
        $html = $this->render("article-tbody",$data);
        // var_dump($html);
        echo $html;      
    }        
    public function fileAction(){
    }
    public function uploadAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $file = new Upload;
        $file->upload();
        // var_dump($file);
    }
    
    
}